<template>
  <el-row>
    <el-card>
      <el-row class="title">
        {{ para.name }}
      </el-row>
      <el-divider />
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('channel.channelBranch.tabData.invitationCode')">
          <el-input v-model="searchForm.invitationCode" :placeholder="$t('channel.prompt.invitationCode')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('channel.channelBranch.tabData.name')">
          <el-input v-model="searchForm.name" :placeholder="$t('channel.prompt.channelBranchName')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('channel.channelBranch.tabData.contact')">
          <el-input v-model="searchForm.contactUser" :placeholder="$t('channel.prompt.contact')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-row>
        <el-button type="primary" :loading="addLoading" @click="add">
          {{ $t('channel.channelBranch.btn.add') }}
        </el-button>
        <el-button type="primary" :loading="generateLoading" @click="generate">
          {{ $t('channel.channelBranch.btn.generate') }}
        </el-button>
        <el-button type="primary" :disabled="!(totalNumber > 0)" @click="exChannelBranch">
          {{ $t('channel.channelBranch.btn.export') }}
        </el-button>
        <el-button type="primary" @click="importChannelBranch">
          {{ $t('channel.channelBranch.btn.import') }}
          <input id="file" type="file" accept=".xls,.xlsx" @change="upLoad">
        </el-button>
      </el-row>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;margin-top: 10px"
      >
        <el-table-column
          fixed="left"
          prop="invitationCode"
          :label="tableText['invitationCode']"
          min-width="100"
        />
        <el-table-column
          fixed="left"
          prop="name"
          :label="tableText['name']"
          min-width="170"
        />
        <el-table-column
          prop="street"
          :label="$t('channel.channelBranch.streetAddress')"
          min-width="170"
        />
        <el-table-column
          prop="contactUser"
          :label="tableText['contact']"
          min-width="170"
        />
        <el-table-column
          prop="contactPhone"
          :label="tableText['contactNumber']"
          min-width="170"
        />
        <el-table-column
          prop="state"
          :label="tableText['status']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['state']) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editItem(scope.row)">
              {{ $t('common.operate.name') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <channel-branch-dialog
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
    <channel-invitation-code-dialog
      :show-dialog="generateDialog"
      :quantity="quantity"
      @change-dialog="changeGenerateDialog"
    />
  </el-row>
</template>

<script>
import { getChannelBranch, invitationCode, unUsedQuantity, channelBranchExport, channelBranchImport } from '@/api/operation'
import ChannelBranchDialog from '@/components/operation/channel-branch-dialog'
import ChannelInvitationCodeDialog from '@/components/operation/channel-invitation-code-dialog'
export default {
  name: 'ChannelBranch',
  components: {
    ChannelBranchDialog,
    ChannelInvitationCodeDialog
  },
  data() {
    return {
      loading: false,
      addLoading: false,
      editDialog: false,
      generateDialog: false,
      generateLoading: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      quantity: 0,
      searchForm: this.params(),
      para: this.$route.query,
      chooseItem: {
        channelId: null,
        id: null,
        invitationCode: null,
        name: null,
        street: null,
        contactUser: null,
        contactPhone: null,
        state: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('channel.channelBranch.tabData')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        channelId: this.$route.query.id,
        invitationCode: null,
        name: null,
        contactUser: null,
        columns: ['state'],
        orderBy: [true]
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    getData() {
      this.loading = true
      const data = this.searchForm
      getChannelBranch(data)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    setValue(val) {
      const name = this.$t('channel.channelStatus').filter(item => {
        return item.value === val
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    add() {
      this.addLoading = true
      invitationCode(this.para.id)
        .then(res => {
          this.addLoading = false
          if (res.success) {
            this.choose({
              invitationCode: res.data
            })
            this.editDialog = true
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.addLoading = false
          this.$message.error(err.message)
        })
    },
    editItem(item) {
      this.choose(item)
      this.editDialog = true
    },
    changeDialog(param) {
      this.editDialog = param
      setTimeout(() => {
        this.choose({})
      }, 10)
    },
    changeGenerateDialog(param) {
      this.generateDialog = param
    },
    editSuccess() {
      this.getData()
    },
    choose(param) {
      this.chooseItem.channelId = this.para.id
      this.chooseItem.id = param.id
      this.chooseItem.invitationCode = param.invitationCode
      this.chooseItem.name = param.name
      this.chooseItem.street = param.street
      this.chooseItem.contactUser = param.contactUser
      this.chooseItem.contactPhone = param.contactPhone
      this.chooseItem.state = param.state
    },
    generate() {
      this.generateLoading = true
      unUsedQuantity(this.para.id)
        .then(res => {
          this.generateLoading = false
          if (res.success) {
            this.quantity = res.data
            this.generateDialog = true
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.generateLoading = false
          this.$message.error(err.message)
        })
    },
    exChannelBranch() {
      channelBranchExport(this.searchForm)
    },
    importChannelBranch() {
      document.getElementById('file').click()
    },
    upLoad() {
      this.loading = true
      const file = document.querySelector('#file')['files'][0]
      const formData = new FormData()
      formData.append('channelId', this.para.id.toString())
      formData.append('file', file)
      channelBranchImport(formData)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.$message.success(this.$t('common.tip.importSuccess'))
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
  .title{
    font-size: 22px;
    line-height: 35px;
  }
  .el-divider{
    margin: 5px 0 15px 0;
  }
  #file{
    position:absolute;
    right: 0px;
    top: 0px;
    opacity: 0;
    -ms-filter: 'alpha(opacity=0)';
  }
</style>
